package dbindex;

import java.sql.*;
import java.util.Set;
import java.util.HashSet;
import java.util.*;import Ontology.*;

public class DomValueAttr {
    public DomValueAttr() {
    }

    /** attr's id in WebDeso ontology */
    public int      attr_value_id;

    /** document's id (autoincremental in the table doc) */
    public int      doc_id;

    /** constant in topic based indexing */
    public float    sim;

    /** id of the row */
    public int    i_id;

    /** id of the ontology */
    public int    onto_id;

    public static void Insert (java.sql.Connection conn,int i_attr_value_id, int i_doc_id, int i_onto_id, double f_sim) 
    {

        int doc_id = 0;

        Statement   s = null;
        ResultSet   rs= null;
        int         size = 0;
        String      str_sql = null;
        DomValueAttr dvaTemp = null;

        try 
        {
            s = conn.createStatement ();
            dvaTemp = Get(conn, i_attr_value_id, i_doc_id, i_onto_id);
            if (dvaTemp == null)
            {
	            str_sql = "INSERT INTO domain_value_attr (attr_value_id, doc_id, onto_id, sim) "+ 
							"VALUES (" + i_attr_value_id + 
									", "+ i_doc_id + 
									", " + i_onto_id +
									", "+ f_sim + ")";
            } else
            {
	            str_sql = "UPDATE domain_value_attr set " + 
	            				" attr_value_id = " + i_attr_value_id + 
	            				", doc_id ="+ i_doc_id + 
	            				", onto_id ="+ i_onto_id + 
	            				", sim = "+ f_sim + 
	            				" WHERE id = "+ dvaTemp.i_id;
            }
            s.executeUpdate (str_sql);
        }catch(SQLException ex) {
            System.err.println("SQLException (WebDeso ComparativeAttr.java Insert()):: sql='" + str_sql + "' " + ex.getMessage());
        } finally {
            if (rs != null) {   try { rs.close(); } catch (SQLException sqlEx) { }  rs = null; }
            if (s != null)  {   try { s.close();  } catch (SQLException sqlEx) { }  s = null;  }
        }
    }

    public static DomValueAttr Get (java.sql.Connection conn,int i_attr_value_id,int i_doc_id, int i_onto_id) {

    	DomValueAttr dva = null;

    	Statement   s = null;
        ResultSet   rs= null;
        int         size = 0;
        String      str_sql = null;

        try {
            s = conn.createStatement ();

            str_sql = "SELECT * FROM domain_value_attr WHERE onto_id = " + i_onto_id +  " and attr_value_id=" + i_attr_value_id + " AND doc_id=" + i_doc_id;
            s.executeQuery (str_sql);

            rs = s.getResultSet ();
            if (rs.next ())
            {
            	dva = new DomValueAttr();
            	dva.attr_value_id = i_attr_value_id;
                dva.doc_id   = i_doc_id;
                dva.onto_id = i_onto_id;
                dva.sim = rs.getFloat("sim");
            }
        } catch(SQLException ex) {
            System.err.println("SQLException (WebDeso ComparativeClass.java Get()):: sql='" + str_sql + "' " + ex.getMessage());
        } finally {
            if (rs != null) {   try { rs.close(); } catch (SQLException sqlEx) { }  rs = null; }
            if (s != null)  {   try { s.close();  } catch (SQLException sqlEx) { }  s = null;  }
        }

        return dva;
    }
    
    public static int[] getAttrValIDByDocID(java.sql.Connection conn,int _doc_id,int _onto_id) 
    {
        Set<Integer> _attr_val_id = new HashSet<Integer>();
            
        Statement   s = null;
        ResultSet   rs= null;
        int         size = 0;
        String      str_sql = null;
        try {
            s = conn.createStatement ();
            
            str_sql = "SELECT * FROM domain_value_attr WHERE doc_id=" + _doc_id + " AND onto_id="+_onto_id;
            s.executeQuery (str_sql);
            
            rs = s.getResultSet ();
            while (rs.next ())
            {
            	_attr_val_id.add(rs.getInt("attr_value_id"));
            }
        } catch(SQLException ex) {
            System.err.println("SQLException (WebDeso ComparativeClass.java getAttrValIDByDocID()):: sql='" + str_sql + "' " + ex.getMessage());
        } finally {
            if (rs != null) {   try { rs.close(); } catch (SQLException sqlEx) { }  rs = null; }
            if (s != null)  {   try { s.close();  } catch (SQLException sqlEx) { }  s = null;  }
        }
        
        Integer[] ri = (Integer[])_attr_val_id.toArray(new Integer[0]);
        int [] r = new int[ri.length];
        for(int i=0;i<ri.length;i++) {
            r[i] = ri[i];
        }
        return r;
    }

    public static boolean getByDocIDAttrID(java.sql.Connection conn,int _doc_id,int _onto_id, Vector iv_attr_val) 
    {

    	boolean b_res = false; 
    	
    	Statement   s = null;
        ResultSet   rs= null;
        int         size = 0;
        String      str_sql = null;
        String		s_attrs = "";        WDTextAttrValues tavTemp;
        
        if (iv_attr_val.size()>0)
        {
        	for (int i=0; i< iv_attr_val.size(); i++)         	{        		tavTemp = (WDTextAttrValues) iv_attr_val.elementAt(i);        		        		s_attrs = s_attrs + ", " + tavTemp.getID();        		        	}


            try {
                s = conn.createStatement ();
                
                str_sql = "SELECT * FROM domain_value_attr WHERE doc_id=" + _doc_id + " AND onto_id="+_onto_id + " and attr_value_id in (" + s_attrs.substring(1) + ")";
                s.executeQuery (str_sql);
                
                rs = s.getResultSet ();
               if (rs.next ())
                {
            	   b_res = true;
                }
            } catch(SQLException ex) {
                System.err.println("SQLException (WebDeso ComparativeClass.java getByDocIDAttrID()):: sql='" + str_sql + "' " + ex.getMessage());
            } finally {
                if (rs != null) {   try { rs.close(); } catch (SQLException sqlEx) { }  rs = null; }
                if (s != null)  {   try { s.close();  } catch (SQLException sqlEx) { }  s = null;  }
            }
        	
        }
        return b_res;
    }
}
